import requests
from bs4 import BeautifulSoup
from typing import List, Dict

def fetch_github_trending(language: str = "python") -> List[Dict]:
    """
    从GitHub Trending获取指定语言的热门项目。

    :param language: 编程语言，默认为'python'
    :return: 包含项目信息的字典列表
    """
    url = f"https://github.com/trending/{language}?since=daily"
    print(f"Fetching trending projects for {language} from GitHub...")
    
    try:
        response = requests.get(url)
        response.raise_for_status()  # 如果请求失败则抛出异常
        
        soup = BeautifulSoup(response.text, "html.parser")
        repos = soup.find_all("article", class_="Box-row")
        
        trending_repos = []
        for repo in repos:
            repo_name_element = repo.find("h2", class_="h3 lh-condensed")
            if not repo_name_element:
                continue
            
            repo_link = repo_name_element.find("a")
            repo_name = repo_link.get_text(strip=True).replace("/", "")
            repo_url = "https://github.com" + repo_link['href']
            
            description_element = repo.find("p", class_="col-9 color-fg-muted my-1 pr-4")
            description = description_element.get_text(strip=True) if description_element else "No description available."
            
            # 获取星星数量
            star_element = repo.find("a", href=f"{repo_link['href']}/stargazers")
            stars = star_element.get_text(strip=True) if star_element else "N/A"

            trending_repos.append({
                "name": repo_name,
                "url": repo_url,
                "description": description,
                "stars": stars
            })
            
        return trending_repos

    except requests.exceptions.RequestException as e:
        print(f"Error fetching GitHub trending projects: {e}")
        return []

if __name__ == '__main__':
    python_trends = fetch_github_trending()
    for repo in python_trends:
        print(f"Repository: {repo['name']}\nStars: {repo['stars']}\nDescription: {repo['description']}\n---")